import useStatAnswerData from '@/store/stat-answer-data'
import {
  QUESTION_TYPE_SINGLE,
  QUESTION_TYPE_MULTI,
  QUESTION_TYPE_SELECT,
  QUESTION_TYPE_TEXT,
  QUESTION_TYPE_TEXTAREA,
  QUESTION_TYPE_MATRIX_RADIO,
} from '@/constants/default'
import { formatTime } from '@/utils/utils'

function formatAnswers(answers) {
  /*
  answers 结构如下:
  [
    {
      id: "",
      survey_id: "",
      collect_time: "",
      answers: [
        {
          id: "",
          type: "",

          text: "", 单行文本 / 多行文本
          options: [ 单选 / 多选 / 下拉
            {id, text}
          ],
          groups: [ 矩阵单选
            {
              id: "",
              options: [
                {id, text}
              ]
            }
          ]
        }
      ]
    }
  ]

  要将上面的结构展开成下面这样子:

  [
    {
      id: 某一份答案id
      collectTime: 答案提交时间
      data: [ 答案
        '问题1答案', '问题2答案', 多选题合并答案, 矩阵单选题分开答案
      ]
    }
  ]
  */

  const answerList = []

  answers.forEach(oneAnswer => {
    const answerLi = {
      id: oneAnswer.id,
      collectTime: formatTime(oneAnswer.collect_time),
      data: [],
    }
    oneAnswer.answer.forEach(a => {
      if (a.type === QUESTION_TYPE_TEXT || a.type === QUESTION_TYPE_TEXTAREA) {
        answerLi.data.push(a.text)
      } else if (
        a.type === QUESTION_TYPE_SINGLE ||
        a.type === QUESTION_TYPE_SELECT
      ) {
        answerLi.data.push(a.options[0]?.text || '')
      } else if (a.type === QUESTION_TYPE_MULTI) {
        let t = ''
        a.options.forEach(o => {
          t += `${o.text}; `
        })
        answerLi.data.push(t)
      } else if (a.type === QUESTION_TYPE_MATRIX_RADIO) {
        a.groups.forEach(g => {
          answerLi.data.push(g.options[0]?.text || '')
        })
      }
    })
    answerList.push(answerLi)
  })

  return answerList
}

function formatSurvey(survey) {
  /*
  survey 的数据结构如下:
  {
    create_time
    id
    info
    title
    questions: [
      id
      type
      order
      title
      required
      options:[ {id, text} ]
      subTitles: [ {id, text} ]
    ]
  }
  要将他们转换为 questionList, 结构如下:
  [
    '问题1', '问题2', 其中矩阵单选将被拆分出来, 由主标题+子标题组成
  ]
  */
  const questionList = []

  survey.questions.forEach(q => {
    if (
      q.type === QUESTION_TYPE_SINGLE ||
      q.type === QUESTION_TYPE_MULTI ||
      q.type === QUESTION_TYPE_SELECT ||
      q.type === QUESTION_TYPE_TEXT ||
      q.type === QUESTION_TYPE_TEXTAREA
    ) {
      questionList.push(q.title)
    } else if (q.type === QUESTION_TYPE_MATRIX_RADIO) {
      q.subTitles.forEach(s => {
        questionList.push(q.title + ': ' + s.text)
      })
    }
  })

  return questionList
}

export default async function () {
  try {
    const statAnswerData = useStatAnswerData()
    const {answers, survey} = await statAnswerData.getData()

    const answerList = formatAnswers(answers)
    const questionList = formatSurvey(survey)

    return {
      surveyTemplate: survey,
      answersTemplate: answers,
      questionList,
      answerList,
      surveyData: {
        id: survey.id,
        title: survey.title,
        createTime: formatTime(survey.create_time),
        questions: survey.questions
      },
    }
  } catch (error) {
    console.error(error.message, error)
    throw error
  }
}
